package control;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import mode.ChessArray;
import util.Reciver;
import util.SocketUtil;
import view.ChessPanel;
import view.GridBagPanel;
import view.InfoPanel;
import view.LoginPanel;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public MainFrame() {
		setTitle("五子棋");
		setSize(1200, 800);
        setLayout(new BorderLayout());
        Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
        Dimension framesize=this.getSize();
        int x=(int)screensize.getWidth()/2-(int)framesize.getWidth()/2;
        int y=(int)screensize.getHeight()/2-(int)framesize.getHeight()/2;
        setLocation(x, y);
        setResizable(false);
        setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		ChessPanel chesspanel=new ChessPanel();
		add(chesspanel);
		
		InfoPanel infoPanel=new InfoPanel();
		add(infoPanel,BorderLayout.EAST);
		
		LoginPanel loginPanel=new LoginPanel();
		setGlassPane(loginPanel);
		loginPanel.setVisible(true);
		
		Reciver reciver=new Reciver();
		reciver.addObserver(loginPanel);
		reciver.addObserver(chesspanel);
		reciver.addObserver(infoPanel);
		
		SocketUtil.getUser().addObserver(infoPanel);
		SocketUtil.getUser().addObserver(chesspanel);
		
		SocketUtil.setReciver(reciver);
		SocketUtil.startServer();
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					UIManager.setLookAndFeel(new NimbusLookAndFeel());
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				new MainFrame();
			}
		});
	}
}
